<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="来访人姓名" prop="name" label-width="100px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来访人手机号" prop="mobile" label-width="100px">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来访时间" prop="time">
        <el-date-picker clearable
          v-model="queryParams.time"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择来访时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- tab导航和新增按钮 -->
    <div class="marg-main newBox">
      <div class="flex-between">
        <div class="tab-wrapper">
          <SwitchBar
            ref="tabBar"
            :data="visitorTabData"
            @change-id="changeId"
          ></SwitchBar>
        </div>
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['nursing:visitor:add']"
        >来访登记</el-button>
      </div>
    </div>

    <el-table v-loading="loading" :data="visitorList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="55" align="center"></el-table-column>
      <el-table-column label="来访类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="visitor_status" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="来访人姓名" align="center" prop="name" />
      <el-table-column label="来访人手机号" align="center" prop="mobile" />
      <el-table-column label="老人姓名" align="center" prop="visitor" />
      <el-table-column label="来访时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改来访登记对话框 -->
    <el-dialog :title="title" v-model="open" width="480px" append-to-body>
      <el-form ref="visitorRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="来访类型" prop="type" required>
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in visitor_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="来访人姓名" prop="name">
          <el-input 
            v-model="form.name" 
            placeholder="请输入" 
            maxlength="10"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="来访人手机号" prop="mobile">
          <el-input 
            v-model="form.mobile" 
            placeholder="请输入" 
            maxlength="11"
            show-word-limit
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="老人姓名" prop="visitor">
          <el-input 
            v-model="form.visitor" 
            placeholder="请输入" 
            maxlength="10"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="来访时间" prop="time">
          <el-date-picker
            v-model="form.time"
            type="datetime"
            placeholder="请选择来访时间"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Visitor">
import SwitchBar from '@/components/switchBar/switchBar.vue';
import { listVisitor, getVisitor, delVisitor, addVisitor, updateVisitor } from "@/api/nursing/visitor";

const { proxy } = getCurrentInstance();
const { visitor_status } = proxy.useDict('visitor_status');

const visitorList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 添加tab数据
const visitorTabData = ref([
  { id: '', name: '全部' },
  { id: '0', name: '参观来访' },
  { id: '1', name: '探访来访' }
]);

// 添加切换方法
const changeId = (val) => {
  queryParams.value.type = val;
  getList();
};

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    mobile: null,
    time: null,
    type: null,  // 添加type字段
  },
  rules: {
    name: [
      { required: true, message: "来访人姓名不能为空", trigger: "blur" }
    ],
    mobile: [
      { required: true, message: "来访人手机号不能为空", trigger: "blur" }
    ],
    time: [
      { required: true, message: "来访时间不能为空", trigger: "blur" }
    ],
    visitor: [
      { required: true, message: "老人姓名不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "来访类型", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询来访登记列表 */
function getList() {
  loading.value = true;
  listVisitor(queryParams.value).then(response => {
    visitorList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    mobile: null,
    time: null,
    visitor: null,
    type: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("visitorRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加来访登记";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getVisitor(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改来访登记";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["visitorRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateVisitor(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVisitor(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除来访登记编号为"' + _ids + '"的数据项？').then(function() {
    return delVisitor(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nursing/visitor/export', {
    ...queryParams.value
  }, `visitor_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>

.newBox {
  background: #fff;
}
.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}
.tab-wrapper {
  width: 300px;
}
</style>
